<?php
namespace app\mouth\common;
use think\App;
use think\Controller;
use think\facade\Cache;
class Api extends Controller{
    public $openid;
    public $portrait;
    public $gender;
    public $quanzi_id;
    public $nickname;
    public $user_id;
    public $session_key;
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: token,Origin, X-Requested-With, Content-Type, Accept");
        header('Access-Control-Allow-Methods: POST,GET');
        $date = Cache::get(input('openid'));

        if ($date['openid'] == false){
            return $this->jsons('40002','请重新登录',[]);
        }
        $this->openid = $date['openid'];
        $this->portrait     =   $date['portrait'];
        $this->nickname     =   $date['nickname'];
        $this->gender       =   $date['portrait'];
        $this->user_id      =   $date['user_id'];
        $this->quanzi_id    =   $date['quanzi_id'];
        $this->session_key  =   $date['session_key'];
    }
    public function jsons($code, $message = '', $data = array())
    {
        //如果状态码不是数字就返回空
        //构造返回数据
        $result = array(
             'code' => $code,
             'message' => $message,
             'data' => $data
         );
        exit(json_encode($result));
//         return json_encode($result);
     }

    public function add($date){
        if ($date){
            return $this->jsons('40006','成功',$date);
        }
        return $this->jsons('40003','失败',[]);
    }


    /**
     * 网址
     * @param string $address   地址
     * @param string $suffix   值
     */
     public static function network($address='',$suffix=''){
        return $_SERVER['REQUEST_SCHEME'] .'://' . $_SERVER['HTTP_HOST'] . $address . $suffix;
     }
}